package com.mk.legoscans2.activities;

import java.util.List;

import com.mk.legoscans2.LegoScans3Application;
import com.mk.legoscans2.R;
import com.mk.legoscans2.Scan;
import com.mk.legoscans2.actionbar.ActionBarActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class ScanListActivity extends ActionBarActivity {
	
	private String _theme;
	private FragmentActivity _activity;
	
	private DownloadScansTask _task;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.scans);
		
		_activity = this;
		
		_theme = getIntent().getStringExtra("theme");
		setTitle(String.format(getResources().getString(R.string.scanlist_title), _theme));
		
		_task = new DownloadScansTask();
		_task.execute();
	}
	
	@Override
	protected void onDestroy() {
		_task.cancel(true);
		LegoScans3Application.TempScans = null;
		super.onDestroy();
	}
	
	private class DownloadScansTask extends AsyncTask<String, Void, List<Scan>> {

		protected List<Scan> doInBackground(String... params) {
			return LegoScans3Application.ApiRepository.getScansByTheme(_theme);
		}
		
		protected void onPostExecute(List<Scan> result) {
			LegoScans3Application.TempScans = result;
			ScanListFragment fragment = (ScanListFragment)_activity.getSupportFragmentManager().findFragmentById(R.id.browse_fragment_container);
			fragment.updateList();
		}
	}
}
